package com.hzzy.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hzzy.IDao.HzDistributionPermissionMapper;
import com.hzzy.IDao.HzMenuMapper;
import com.hzzy.bean.BaseInfo;
import com.hzzy.bean.HzDistributionPermission;
import com.hzzy.bean.HzDistributionPermissionExample;
import com.hzzy.bean.HzMenu;
import com.hzzy.bean.HzMenuFunction;
import com.hzzy.bean.HzStudent;
import com.hzzy.bean.HzTeacher;
import com.hzzy.entity.MenuEntity;
import com.hzzy.service.HzMenuService;

import jxl.common.Logger;

@Service
public class HzMenuServiceImpl implements HzMenuService {
	private static Logger log = Logger.getLogger(HzMenuServiceImpl.class);
	@Autowired
	private HzMenuMapper hzMenuMapper;
	@Autowired
	private HzDistributionPermissionMapper hzDistributionPermissionMapper;
	
	public BaseInfo matchingPermission(HzStudent student) {
		BaseInfo baseInfo = new BaseInfo();
		baseInfo.setStudent(student);
		baseInfo.setStudentUser(true);
		Map<String, Object> conditions = new HashMap<String, Object>();
		conditions.put("classId", student.getClassid());
		conditions.put("sno", student.getSno());
		// 上课点到生权限
		List<HzMenu> menuList = hzMenuMapper.selectStudentMenuByContidions(conditions);
		// 其他职位权限
		conditions.put("notIncludeCheckCardreId", student.getHzClasses().getClasscheckCadreid());
		List<HzMenu> _menuList = hzMenuMapper.selectStudnetMenuByDormitoryLeader(conditions);
		if (_menuList != null && _menuList.size() > 0) {
			menuList.addAll(_menuList);
		}
		if (menuList != null && menuList.size() > 0) {
			baseInfo.setMenuList(menuList);
			baseInfo.setAllowAccessUri(getAllowAccessUri(menuList));
		}
		return baseInfo;
	}

	public BaseInfo matchingPermission(HzTeacher teacher) {
		BaseInfo baseInfo = new BaseInfo();
		baseInfo.setTeacher(teacher);
		baseInfo.setStudentUser(false);
		Map<String, Object> conditions = new HashMap<String, Object>();
		conditions.put("teacherId", teacher.getTeacherId());
		List<HzMenu> menuList = hzMenuMapper.selectTeacherMenuByConditions(conditions);
		if (menuList != null && menuList.size() > 0) {
			baseInfo.setMenuList(menuList);
			baseInfo.setAllowAccessUri(getAllowAccessUri(menuList));
		}
		return baseInfo;
	}

	private List<String> getAllowAccessUri (List<HzMenu> menuList) {
		List<String> allowAccessUri = new ArrayList<String>();
		for (HzMenu menu : menuList) {	//主菜单页面
			if (menu.getSubmenuList() != null && menu.getSubmenuList().size() > 0) {
				
				for (HzMenu subMenu : menu.getSubmenuList()) {	//子菜单
					allowAccessUri.add(subMenu.getMenuUri());
					if (subMenu.getSubMenuFunctionList() != null && subMenu.getSubMenuFunctionList().size() > 0) {
						
						for (HzMenuFunction function : subMenu.getSubMenuFunctionList()) {	//功能页面的链接
							allowAccessUri.add(function.getFunctionUrl());
						}
					}
				}
				
			}
		}
		return allowAccessUri;
	}

	public List<MenuEntity> getMenuEntityList(Integer typeId) {
		Map<String, Object> conditions = new HashMap<String, Object>();
		List<HzMenu> menuList = hzMenuMapper.selectAllParentMenu(conditions);
		
		// 用于判断是否以选中
		HzDistributionPermissionExample example = new HzDistributionPermissionExample();
		example.createCriteria().andPermissionTypeIdEqualTo(typeId);
		List<HzDistributionPermission> dpList = hzDistributionPermissionMapper.selectByExample(example);
		
		List<MenuEntity> entityList = new ArrayList<MenuEntity>();
		if (menuList != null && menuList.size() > 0) {
			for (HzMenu menu : menuList) {
				MenuEntity entity = new MenuEntity();
				
				entity.setMenuId(menu.getId() + "");
				entity.setText(menu.getMenuName());
				// 判断是否有子菜单
				if (menu.getSubmenuList() != null && menu.getSubmenuList().size() > 0) {
					List<MenuEntity> nodesList = new ArrayList<MenuEntity>(); 
					for (HzMenu subMenu : menu.getSubmenuList()) {
						MenuEntity subEntity = new MenuEntity();
						subEntity.setMenuId(subMenu.getId() + "");
						subEntity.setParent(menu.getMenuName());
						subEntity.setText(subMenu.getMenuName());
						if (dpList != null && dpList.size() > 0) {
							subEntity.setChecked(isChecked(dpList, subMenu.getId()));
						} else {
							subEntity.setChecked(false);
						}
						nodesList.add(subEntity);
					}
					entity.setNodes(nodesList);
				}
				
				// 是否已选择
				entity.setChecked(isChecked(dpList, menu.getId()));
				if (dpList != null && dpList.size() > 0) {
					isChecked(dpList, menu.getId());
				}
				
				entityList.add(entity);
			}
		}
		return entityList;
	}

	public List<MenuEntity> getMenuEntityListByPermissionTypeId(Integer typeid) {
		// TODO Auto-generated method stub
		return null;
	}
	
	/**
	 * 是否已选中
	 * @return
	 */
	private boolean isChecked(List<HzDistributionPermission> dpList, Integer menuId) {
		for (HzDistributionPermission dp : dpList) {
			log.debug(dp.getMenuId() + " " + menuId);;
			if (dp.getMenuId() == menuId) {
				log.debug("true");
				return true;
			}
		}
		return false;
	}
}
